package ai.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AI配置类
 * 配置通义大模型和ChatClient
 */
@Configuration
public class AiConfig {
    
    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;
    
    @Value("${spring.ai.dashscope.chat.model:qwen-plus}")
    private String model;
    
    @Value("${spring.ai.dashscope.chat.temperature:0.7}")
    private Double temperature;
    
    // 某些版本的 DashScopeChatOptions 不支持 maxTokens，此处先不使用
    
    /**
     * 配置 DashScope API 客户端
     */
    @Bean
    public DashScopeApi dashScopeApi() {
        return new DashScopeApi(apiKey);
    }

    /**
     * 配置 DashScope 聊天模型
     */
    @Bean
    public DashScopeChatModel dashScopeChatModel(DashScopeApi dashScopeApi) {
        DashScopeChatOptions options = DashScopeChatOptions.builder()
                .withModel(model)
                .withTemperature(temperature)
                .build();
        return new DashScopeChatModel(dashScopeApi, options);
    }
    
    /**
     * 配置一个ChatClient
     */
    @Bean
    public ChatClient chatClient(DashScopeChatModel dashScopeChatModel) {
        return ChatClient.builder(dashScopeChatModel)
                .defaultSystem("你是一个专业的酒店管理AI助手，可以帮助用户搜索酒店、预订房间、查询价格等。请用中文回答用户的问题。")
                .build();
    }
}
